package com.celesea.auth.form.controller;

import com.celesea.auth.dept.service.DeptService;
import com.celesea.auth.dept.vo.DeptTreeVo;
import com.celesea.auth.form.controller.resource.NodeResource;
import com.celesea.auth.role.entity.RoleCode;
import com.celesea.auth.user.entity.User;
import com.celesea.auth.user.entity.UserPerm;
import com.celesea.auth.user.service.UserPermService;
import com.celesea.auth.user.service.UserService;
import com.celesea.auth.user.vo.UserFilter;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.StringKit;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-11-02 22:31
 */
@RestController
@RequestMapping("v1/depts")
@Api(tags = "部门选择接口")
public class FormController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserPermService userPermService;

    @GetMapping("deptNodes")
    @ApiOperation(value = "部门树节点")
    public List<NodeResource> deptNodes(String parentId) {
        List<DeptTreeVo> nodes = deptService.selectNodes(parentId);
        return nodes.stream().map(item -> new NodeResource(item)).collect(Collectors.toList());
    }

    @GetMapping("deptUserNodes")
    @ApiOperation(value = "部门员工树节点")
    public List<NodeResource> deptUserNodes(String parentId) {
        List<NodeResource> nodes = Lists.newArrayList();
        List<DeptTreeVo> depts = deptService.selectNodes(parentId);
        if (CollectionKit.isNotEmpty(depts)) {
            depts.stream().forEach(item -> nodes.add(new NodeResource(item)));
        }
        if (StringKit.isNotEmpty(parentId)) {
            UserFilter filter = new UserFilter(parentId);
            List<User> users = userService.selectByDept(filter);
            boolean isAdminSystem = EnvUtil.hasAnyRoles(RoleCode.ADMIN_SYSTEM);
            if (CollectionKit.isNotEmpty(users)) {
                users.stream().forEach(item -> {
                    if (Const.Bool.isTrue(item.getPermFlag())) {
                        if (isAdminSystem || isPermUser(item)) {
                            nodes.add(new NodeResource(item));
                        }
                    } else {
                        nodes.add(new NodeResource(item));
                    }
                });
            }
        }
        return nodes;
    }

    private boolean isPermUser(User user) {
        if (EnvUtil.getAuthorityUser().getUser().getId().equals(user.getId())) {
            return true;
        }
        List<UserPerm> userPerms = userPermService.findByUser(user.getId());
        if (CollectionKit.isNotEmpty(userPerms)) {
            String userId = EnvUtil.getLoginUser().getId();
            String deptId = EnvUtil.getLoginDept().getId();
            for (UserPerm item : userPerms) {
                if ((UserPerm.PERM_TYPE_USER == item.getPermType() && userId.equals(item.getPermId())) ||
                        (UserPerm.PERM_TYPE_DEPT == item.getPermType() && deptId.equals(item.getPermId()))) {
                    return true;
                }
            }
        }
        return false;
    }
}
